var Sjfx = function() {

	var createTable = function(cwmx, canghao, time) {
		var $table = $("<table class='cwmx'></table>");
		var tr = "";

		var $secondRow = $("<tr class='hang'><td></td><td>行号</td></tr>");

		var y_pre = "";
		var z_pre = "";
		var tr = "<tr>";
		var count = 0;
		$.each(cwmx, function(n, value) {
			var x = value.x;
			var y = value.y;
			var z = value.z;
			var wendu = value.wendu;
			if (y != y_pre) {
				if (n != 0) {
					if (count == 0) {
						count = n + 2;
						var firstRow = "<tr class='cang'>";
						firstRow = firstRow + "<td class='first' colspan='" + count + "'>" + canghao + "号仓         " + time
								+ "         测温明细" + "</td>";
						firstRow = firstRow + "</tr>"
						$table.append($(firstRow));

						for (var i = 1; i <= count - 2; i++) {
							$secondRow.append($("<td>" + i + "</td>"));
						}
						$table.append($secondRow);
					}
					tr = tr + "</tr>";
					var $tr = $(tr);
					$table.append($tr);

					tr = "<tr>";
				}
				if (z != z_pre) {
					if (n != 0) {
						tr = tr + "<td>" + "统计" + "</td>";
						tr = tr + "<td class='cwen' colspan='" + count + "'>" + "层最高温：" + 11 + "  层最低温：" + 11 + "层平均温：" + 11
								+ "相邻点最大温差：" + 12 + "</td>";
						$table.append($(tr));

						tr = "<tr>";
					}
					tr = tr + "<td>" + z + "层</td>";
					tr = tr + "<td>" + y + "</td>";
					tr = tr + "<td>" + wendu + "</td>";
				} else {
					tr = tr + "<td></td>";
					tr = tr + "<td>" + y + "</td>";
					tr = tr + "<td>" + wendu + "</td>";
				}
			} else {
				tr = tr + "<td>" + wendu + "</td>";
			}
			y_pre = y;
			z_pre = z;
		});
		tr = tr + "</tr>";
		$table.append($(tr));

		tr = "<tr>";
		tr = tr + "<td>" + "统计" + "</td>";
		tr = tr + "<td class='cwen' colspan='" + count + "'>" + "层最高温：" + 11 + "  层最低温：" + 11 + "层平均温：" + 11 + "相邻点最大温差：" + 12
				+ "</td>";
		$table.append($(tr));

		$("#detail").append($table);
	}

	var getCwmx = function() {
		$.ajax({
			type : "GET",
			cache : false,
			url : "sjfx/cwmx",
			success : function(responseJson) {
				if (responseJson.success == true) {// 返回true
					var cwmxBegin = responseJson.obj.sjfxCwmxBegin;
					createTable(cwmxBegin, 1, "2017-03-03 11:00:00");
					var cwmxEnd = responseJson.obj.sjfxCwmxEnd;
					createTable(cwmxEnd, 2, "2017-03-03 12:00:00");
				} else {
					QTYYComponents.bootstrapSweetAlert("", responseJson.msg, "error");
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				QTYYComponents.bootstrapSweetAlert("", "查询失败!!!", "error");
			}
		});
	}

	var drawSanwen = function(sanwen, detail) {
		var time = [];
		var cangwen = [];
		var waiwen = [];
		var liangwen = [];

		$.each(sanwen, function(n, value) {
			time.push(value.time);
			cangwen.push(value.cangwen * 1);
			waiwen.push(value.waiwen * 1);
			liangwen.push(value.liangwen * 1);
		});

		$('#' + detail).highcharts({
			chart : {
				type : 'spline'
			},
			title : {
				text : '三温图'
			},
			xAxis : {
				type : 'linear',
				tickInterval : 1,
				data : time,
				gridLineDashStyle : 'ShortDot'
			},
			yAxis : {
				type : 'linear',
				labels : {
					formatter : function() {
						return this.value + '℃';
					}
				}
			},
			tooltip : {
				crosshairs : true,
				shared : true
			},
			plotOptions : {
				spline : {
					marker : {
						radius : 4,
						lineColor : '#666666',
						lineWidth : 1
					},
				}
			},
			series : [ {
				name : '外温',
				marker : {
					fillColor : '#1C1C1C',
					symbol : 'square',
					radius : 2
				},
				lineColor : '#1C1C1C',
				data : waiwen
			}, {
				name : '仓温',
				marker : {
					fillColor : '#00FFFF',
					symbol : 'triangle',
					radius : 2
				},
				lineColor : '#00FFFF',
				data : cangwen
			}, {
				name : '粮温',
				marker : {
					fillColor : '#FF0000',
					symbol : 'circle',
					radius : 2
				},
				lineColor : '#FF0000',
				data : liangwen
			} ]
		});
	}

	var getSanwen = function() {
		$.ajax({
			type : "GET",
			cache : false,
			url : "sjfx/sanwen",
			success : function(responseJson) {
				if (responseJson.success == true) {// 返回true
					var detailBegin = $("<div id=\"detailBegin\" style=\"width: 100%; min-height: 300px;\"></div>");
					$("#detail").append(detailBegin);
					var sanwenBegin = responseJson.obj.sjfxSanwenBegin;
					drawSanwen(sanwenBegin, "detailBegin");
					var detailEnd = $("<div id=\"detailEnd\" style=\"width: 100%; min-height: 300px;\"></div>")
					$("#detail").append(detailEnd);
					var sanwenEnd = responseJson.obj.sjfxSanwenEnd;
					drawSanwen(sanwenEnd, "detailEnd");
				} else {
					QTYYComponents.bootstrapSweetAlert("", responseJson.msg, "error");
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				QTYYComponents.bootstrapSweetAlert("", "查询失败!!!", "error");
			}
		});
	}

	/**
	 * 绑定按钮事件
	 */
	var eventHandler = function() {
		$("#cwmxBtn").on("click", function() {
			$("#detail").empty();
			getCwmx();
		});
		$("#sanwenBtn").on("click", function() {
			$("#detail").empty();
			getSanwen();
		});
	}

	return {
		init : function() {
			eventHandler();
			getCwmx();
		}
	}
}();

jQuery(document).ready(function() {
	Sjfx.init();
});
